package servlet;

import com.alibaba.fastjson.JSON;
import dao.GoodsDao;
import dao.impl.GoodsDaoImpl;
import pojo.entity.Goods;
import util.GetPost;
import util.JDBCDruidUtil;
import util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        GoodsDao dao = new GoodsDaoImpl();

        switch(action){
            case "querySearchShopGoodsList":
                Map<String,Object> map = new HashMap<>();
                String goodsName = req.getParameter("goodsName");
                String goodsStatus = req.getParameter("goodsStatus");
                String shopName = req.getParameter("shopName");

                if(req.getParameter("pageNum") != null){
                    int pageNum = Integer.parseInt(req.getParameter("pageNum"));
                    map.put("pageNum",pageNum);
                }
                if(req.getParameter("pageSize") != null){
                    int pageSize = Integer.parseInt(req.getParameter("pageSize"));
                    map.put("pageSize",pageSize);
                }
                String startTime = req.getParameter("startTime");
                String endTime = req.getParameter("endTime");
                map.put("goodsName",goodsName);
                map.put("goodsStatus",goodsStatus);
                map.put("shopName",shopName);
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                List<Goods> list = dao.selectAll(map);
                int total = dao.count(map);
                JDBCDruidUtil.close();

                Map<String,Object> result = new HashMap<>();
                result.put("list",list);
                result.put("total",total);
                JDBCDruidUtil.close();
                resp.getWriter().write(JSON.toJSONString(Result.success(result)));


            default:break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        GoodsDao dao = new GoodsDaoImpl();
        Result result = null;
        switch(action){
            case "modifyShopGoods":
                GetPost<Goods> get= new GetPost<>();
                Goods goods = get.getPost(req, Goods.class);
                boolean right = dao.modify(goods);
                JDBCDruidUtil.close();
                if(right){
                    result = Result.success();
                }else{
                    result = Result.error();
                }
                resp.getWriter().write(JSON.toJSONString(result));
            default:break;
        }


    }
}
